package com.chuangke.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chuangke.admin.data.builder.UserDataBuilder;
import com.chuangke.common.data.builder.DataBuildConfig;
import com.chuangke.common.data.builder.DataBuilder;
import com.chuangke.system.dao.SysRelationDao;
import com.chuangke.system.entity.SysRelation;
import com.chuangke.system.service.SysRelationService;

@Service
public class SysRelationServiceImpl implements SysRelationService {

	@Autowired
	private SysRelationDao sysRelationDaoService;

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void save(String subjectId, String relationType, String relationId) {
		SysRelation sr = new SysRelation();
		sr.setSubjectId(subjectId);
		sr.setRelationType(relationType);
		sr.setRelationId(relationId);
		this.save(sr);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void save(SysRelation sysRelation) {
		sysRelationDaoService.save(sysRelation);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void saveBatch(List<SysRelation> list) {
		sysRelationDaoService.saveBatch(list);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void delete(String subjectId, String relationId) {
		sysRelationDaoService.remove(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getSubjectId, subjectId)
				.eq(SysRelation::getRelationId, relationId));
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void deleteBySubjectId(String subjectId) {
		sysRelationDaoService.remove(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getSubjectId, subjectId));
	}
	
	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void deleteBySubjectIds(List<String> subjectIds, String relationType) {
		QueryWrapper<SysRelation> qw = new QueryWrapper<SysRelation>() ;
		qw.lambda().in(SysRelation::getSubjectId, subjectIds) ;
		qw.lambda().eq(SysRelation::getRelationType, relationType) ;
		sysRelationDaoService.remove(qw);
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void deleteBySubjectIds(List<String> subjectIds) {
		if(CollectionUtils.isEmpty(subjectIds)) {
			return ;
		}
		
		sysRelationDaoService
				.remove(new QueryWrapper<SysRelation>().lambda().in(SysRelation::getSubjectId, subjectIds));
	}

	@Override
	@Transactional(rollbackFor = Throwable.class)
	public void deleteByRelationId(String relationId) {
		sysRelationDaoService
				.remove(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getRelationId, relationId));
	}
	
	@Override
	public void deleteBySubjectId(String subjectId, String relationType) {
		QueryWrapper<SysRelation> qw = new QueryWrapper<SysRelation>() ;
		qw.lambda().eq(SysRelation::getSubjectId, subjectId) ;
		qw.lambda().eq(SysRelation::getRelationType, relationType) ;
		sysRelationDaoService.remove(qw);
	}

	@Override
	public List<String> findRelationIdsBySubjectId(String subjectId) {
		List<SysRelation> list = sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getSubjectId, subjectId));
		return list.stream().map(SysRelation::getRelationId).collect(Collectors.toList());
	}
	
	@Override
	public List<String> findRelationIdsByRelationType(String subjectId,String relationType){
		List<SysRelation> list = sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getSubjectId, subjectId).eq(SysRelation::getRelationType, relationType));
		return list.stream().map(SysRelation::getRelationId).collect(Collectors.toList());
	}
	
	@Override
	public List<String> findRelationIdsByRelationTypes(List<String> subjectIds,List<String> relationTypes){
		List<SysRelation> list = sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().in(SysRelation::getSubjectId, subjectIds).in(SysRelation::getRelationType, relationTypes));
		return list.stream().map(SysRelation::getRelationId).collect(Collectors.toList());
	}

	@Override
	public List<SysRelation> findBySubjectId(String subjectId) {
		List<SysRelation> list = sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().eq(SysRelation::getSubjectId, subjectId));
		DataBuilder.of(list)
		.appendBuilder(new UserDataBuilder(), new DataBuildConfig("createdBy", "createdByName"),
				new DataBuildConfig("updatedBy", "updatedByName")).build();

		return list;
	}

	@Override
	public List<SysRelation> findBySubjectIds(List<String> subjectIds) {
		if(CollectionUtils.isEmpty(subjectIds)) {
			return new ArrayList<>() ;
		}

        return sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().in(SysRelation::getSubjectId, subjectIds));
	}
	
	@Override
	public List<SysRelation> findByByRelationTypes(List<String> subjectIds,List<String> relationTypes){
		List<SysRelation> list = sysRelationDaoService
				.list(new QueryWrapper<SysRelation>().lambda().in(SysRelation::getSubjectId, subjectIds).in(SysRelation::getRelationType, relationTypes));
		return list;
	}
	
	
	@Override
	public List<SysRelation> list(QueryWrapper<SysRelation> qw) {
        return sysRelationDaoService .list(qw);
	}

}
